-- Functions useful for writing cross-platform tests
-- Copyright 2004 (C) Ralph Thomas

--
-- findChild( parent, name )
--
-- This function returns the access object which has the name
-- specified, and is a child of the given parent object. This
-- function attempts to find the deepest child with the given
-- name (including subchildren of matching children).
--
-- \param	parent	the access object to look at the children of
-- \param	name	the name of the child to look for
-- \param	recurse	should the function check subchildren
--
-- \return	an access table, or nil if there were no matching
--		children.
--
function findChild( parent, name, recurse )
	local i = 0
	while i < parent:numberChildren() do
		local ch = parent:child( i )
		if ch ~= nil then
			--
			-- If this name is the one we're after then check
			-- it's children to make sure that we have the
			-- actual object, and not just the container.
			--
			if ch:name() == name then
				local subch = findChild( ch, name, false )
				if subch ~= nil then return subch end
				return ch
			end
			--
			-- Recurse deeper into the tree, returning anything
			-- we find.
			--
			if recurse == true then
				local subch = findChild( ch, name, true )
				if subch ~= nil then return subch end
			end
		end
		i = i + 1
	end
	--
	-- Went through the whole tree without finding anything, return
	--
	return nil
end

--
-- closeWindow( window )
--
-- This function will locate the close button of a window, and then send
-- a click event to it. This should result in the window being closed.
--
-- \param	window	the window to close
--
function closeWindow( window )
	--
	-- MSAA (Windows) calls the close button "Close".
	--
	local closeButton = findChild( window, "Close", true )
	if closeButton ~= nil then closeButton:sendClick() end
	--
	-- XXX: Insert Apple window close function.
	--
end

